//
//  TreeHoleVideoPlayViewController.m
//  togetherPlay
//
//  Created by coder on 2018/12/21.
//  Copyright © 2018 stoneobs.qq.com. All rights reserved.
//

#import "THVideoDetailViewController.h"
#import "STTools_PlayControlView.h"
#import "THLookVideoCommentViewController.h"
#import "dian_ji_Tools_AddReProportMJ_SViewController.h"
@interface THVideoDetailViewController ()

@property(nonatomic, strong) ZFPlayerController                     *player;/**<  */
@property (nonatomic, strong) ZFPlayerControlView                  *controlView;
@property(nonatomic, strong) UIView                                *fatherView;/**<  */

@property(nonatomic, strong) UIView                                *moveView;/**<  */


@property(nonatomic, strong) ZFPlayerController                     *below_layer;/**<  */
@property (nonatomic, strong) ZFPlayerControlView                  *below_controlView;
@property(nonatomic, strong) UIView                                *below_fatherView;/**<  */

@property(nonatomic, strong) STLabel                     *goodLabel;/**<  */
@property(nonatomic, strong) STLabel                     *commentLabel;/**<  */
@property(nonatomic, strong) STLabel                     *shareLabel;/**<  */
@end

@implementation THVideoDetailViewController
- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    self.view.backgroundColor = UIColor.blackColor;
    self.tableView.backgroundColor = UIColor.clearColor;
    self.view.backgroundColor = UIColor.blackColor;
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.navigationController.navigationBar.hidden = YES;

    
    [self reloadData];
    
   
    
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self.player.currentPlayerManager play];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    [self.player.currentPlayerManager pause];
}
- (void)reloadData{
    [self.fatherView.superview removeFromSuperview];
    [self.moveView removeFromSuperview];
    [self.cus_navgationBar removeFromSuperview];
    self.cus_navgationBar.backgroundColor = UIColor.clearColor;
    self.cus_navgationBar.rightButton.hidden = YES;
    [self.cus_navgationBar.leftButton setImage:ST_IMAGE_NAMED(@"video_返回") forState:UIControlStateNormal];
    [self.view addSubview:self.cus_navgationBar];
    
    [self stoneobs_ui_configSubView];
    [self config_treeHole_view];
    [self.view bringSubviewToFront:self.cus_navgationBar ];
    ({
        UISwipeGestureRecognizer * swip = [[UISwipeGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            UISwipeGestureRecognizer * mysender = sender;
            NSInteger index = [self.dataSouce indexOfObject:self.model];
            if (mysender.direction == UISwipeGestureRecognizerDirectionLeft) {
                NSLog(@"left");
                if (index<(self.dataSouce.count-1)) {
                    index = index + 1;
                    self.model = self.dataSouce[index];
                    [self reloadData];
//                    [self.view stoneobs_fun_st_showAnimationWithType:STAnimationTypeSTpageCurle_stoneobs_enum];
                    
                }
            }
        }];
        swip.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.moveView addGestureRecognizer:swip];
    });
    ({
        UISwipeGestureRecognizer * swip = [[UISwipeGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
            UISwipeGestureRecognizer * mysender = sender;
            NSInteger index = [self.dataSouce indexOfObject:self.model];
            if (mysender.direction == UISwipeGestureRecognizerDirectionRight ) {
                NSLog(@"right");
                if (index>0) {
                    index = index - 1;
                    self.model = self.dataSouce[index];
                    [self reloadData];
//                    [self.view stoneobs_fun_st_showAnimationWithType:STAnimationTypeSTpageCurle_stoneobs_enum];
                }
            }
        }];
        swip.direction = UISwipeGestureRecognizerDirectionRight;
        [self.moveView addGestureRecognizer:swip];
    });
}
#pragma mark --subView
- (void)stoneobs_ui_configSubView{


    
    ({
        //        UIImageView * imageView = [[UIImageView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
        //        [imageView sd_setImageWithURL:self.img_url.stoneobs_fun_st_safeImageUrl];
        //        [self.view insertSubview:imageView atIndex:0];
        //        self.imageView = imageView;
        //        imageView.userInteractionEnabled = NO;
        //        UIVisualEffectView * visouView = [[UIVisualEffectView alloc] initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
        //        visouView.frame = imageView.bounds;
        //        [self.view addSubview:visouView];
        
        //        self.below_fatherView = self.imageView;
        //        self.below_fatherView.backgroundColor = UIColor.blackColor;
        //         ZFAVPlayerManager * avmanger =  ZFAVPlayerManager.new;
        //        self.below_layer = [ZFPlayerController playerWithPlayerManager:avmanger containerView:self.below_fatherView];
        //        self.below_layer.assetURL = [NSURL URLWithString:self.url];
        //        self.below_layer.pauseWhenAppResignActive = YES;
        //        self.below_layer.controlView = self.below_controlView;
        //        self.below_layer.currentPlayerManager.muted = YES;
    });
    
    ({
        // 初始化
        UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, STONEOBS_UI_UISCREEN_HEIGHT)];
        self.fatherView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, STONEOBS_UI_UISCREEN_WIDTH, STONEOBS_UI_UISCREEN_HEIGHT)];
        self.fatherView.backgroundColor = UIColor.blackColor;                                                                                        self.fatherView.centerY = header.height/2;
        [header addSubview:self.fatherView];

        
//        self.fatherView.userInteractionEnabled = NO;
        ZFAVPlayerManager * avmanger =  ZFAVPlayerManager.new;
        self.player = [ZFPlayerController playerWithPlayerManager:avmanger containerView:self.fatherView];
        self.player.assetURL = [NSURL URLWithString:self.model.url];
        self.player.pauseWhenAppResignActive = YES;
        self.player.controlView = self.controlView;
        
        [self.view addSubview:header];
        __weak typeof(self) weakSelf =  self;
        [self.player.currentPlayerManager setPlayerPlayTimeChanged:^(id<ZFPlayerMediaPlayback>  _Nonnull asset, NSTimeInterval currentTime, NSTimeInterval duration) {
            
//            NSLog(@"currentTime = %f,duration = %f",currentTime,duration);
        }];
        [self.player.currentPlayerManager setPlayerPlayStateChanged:^(id<ZFPlayerMediaPlayback>  _Nonnull asset, ZFPlayerPlaybackState playState) {
            
            
        }];
        [self.player.currentPlayerManager setPlayerDidToEnd:^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
           
            [weakSelf.player.currentPlayerManager replay];
        }];
    });
    
    
    
}

- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
        _controlView.autoHiddenTimeInterval = 5;
        _controlView.autoFadeTimeInterval = 0.5;
        _controlView.prepareShowLoading = YES;
        _controlView.prepareShowControlView = YES;
        _controlView.userInteractionEnabled = NO;
    }
    return _controlView;
}
- (ZFPlayerControlView *)below_controlView {
    if (!_below_controlView) {
        _below_controlView = [ZFPlayerControlView new];
        _below_controlView.fastViewAnimated = YES;
        _below_controlView.autoHiddenTimeInterval = 5;
        _below_controlView.autoFadeTimeInterval = 0.5;
        _below_controlView.prepareShowLoading = YES;
        _below_controlView.prepareShowControlView = YES;
        
    }
    return _controlView;
}
- (void)config_treeHole_view{
    __weak typeof(self) weakSelf =  self;

    
    STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, 0, 80, 30)
                                                              title:@""
                                                         titleColor:UIColor.whiteColor
                                                          titleFont:14
                                                       cornerRadius:0
                                                    backgroundColor:nil
                                                    backgroundImage:nil
                                                              image:ST_IMAGE_NAMED(@"more_white")];

    stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [stoneobs_ui_button setTitleColor:STONEOBS_UI_THEME_BACKGROUNDCOLOR forState:UIControlStateSelected];
    stoneobs_ui_button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    stoneobs_ui_button.tag = 999888;
    [[self.cus_navgationBar viewWithTag:999888] removeFromSuperview];
    [self.cus_navgationBar addSubview:stoneobs_ui_button];
    stoneobs_ui_button.selected = self.model.is_collection;
    [stoneobs_ui_button setClicAction:^(UIButton *sender) {
        [weakSelf stoneobs_fun_st_rightBarAction:nil];
    }];
    stoneobs_ui_button.bottom = self.cus_navgationBar.height;
    stoneobs_ui_button.right = UISCREEN_WIDTH - 10;
    
    //覆盖view
    UIView * contentView = [UIView.alloc initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    self.moveView = contentView;
    [self.view addSubview:contentView];
    
    
    STButton * comment_button = [[STButton alloc] initWithFrame:CGRectMake(15, TMUtils.stoneobs_fun_get_tabBarTop,UISCREEN_WIDTH - 30, 42)
                                                          title:@"说点什么"
                                                     titleColor:UIColor.whiteColor
                                                      titleFont:15
                                                   cornerRadius:21
                                                backgroundColor:[WH_UICOLOR_FROM_RGBA(0x343434) colorWithAlphaComponent:0.21]
                                                backgroundImage:nil
                                                          image:nil];
    [contentView addSubview:comment_button];
    [comment_button setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedCommentButton];
    }];
    comment_button.titleEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
    comment_button.titleLabel.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:15];
    
    CGFloat top = comment_button.top - 20;
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, 200, 20)
                                                                text:self.model.title
                                                           textColor:UIColor.whiteColor
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [contentView addSubview:stoneobs_ui_label];
        stoneobs_ui_label.font = [UIFont fontWithName:STONEOBS_UI_FONT_CU_NAME size:12];
        [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stoneobs_ui_label];
        if (stoneobs_ui_label.height > 40) {
            stoneobs_ui_label.height = 40;
        }
        stoneobs_ui_label.bottom = top;
    
        top = stoneobs_ui_label.top;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(15, top, 200, 20)
                                                                text:self.model.created_at
                                                           textColor:UIColor.whiteColor
                                                                font:11
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentLeft];
        [contentView addSubview:stoneobs_ui_label];
        stoneobs_ui_label.bottom = top;
        top = stoneobs_ui_label.top - 30;
    });
    
    if (self.model.goods_name.length) {
        CGFloat witdh = [self.model.goods_name stoneobs_fun_st_widthWithheight:20 font:12];
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(15, top, witdh + 30 + 40 + 20, 30)
                                                                  title:self.model.goods_name
                                                             titleColor:STONEOBS_UI_SECEND_TEXT_COLOR
                                                              titleFont:15
                                                           cornerRadius:15
                                                        backgroundColor:WH_UICOLOR_FROM_RGBA(0xF6F6F6)
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"video_标签")];
        [contentView addSubview:stoneobs_ui_button];
        stoneobs_ui_button.contentEdgeInsets = UIEdgeInsetsMake(0, 15, 0, 0);
        stoneobs_ui_button.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            [TMUtils goto_good_detail_witdh_id:weakSelf.model.goods_id];
        }];
        stoneobs_ui_button.bottom = top;
        top = stoneobs_ui_button.top;
    }
    
    ({
        
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)
                                                                text:@(self.model.share_num).description
                                                           textColor:UIColor.whiteColor
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
        [contentView addSubview:stoneobs_ui_label];
        
        self.shareLabel = stoneobs_ui_label;
        stoneobs_ui_label.bottom = top;
        top = stoneobs_ui_label.top;
        
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, top, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:20
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"video_分享")];
        [contentView addSubview:stoneobs_ui_button];
        stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [stoneobs_ui_button setImage:ST_IMAGE_NAMED(@"video_分享") forState:UIControlStateSelected];
        stoneobs_ui_button.right = UISCREEN_WIDTH - 15;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            
            [TMUtils goto_share_witdh_url:weakSelf.model.t_id];
        }];
        stoneobs_ui_button.bottom = top;
        top = stoneobs_ui_button.top;
        stoneobs_ui_label.centerX = stoneobs_ui_button.centerX;
    });
    
    ({
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)
                                                                text:@(self.model.comment_num).description
                                                           textColor:UIColor.whiteColor
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
        [contentView addSubview:stoneobs_ui_label];

        self.commentLabel = stoneobs_ui_label;
        stoneobs_ui_label.bottom = top;
        top = stoneobs_ui_label.top;
        
        
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, top, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:20
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"video_评论")];
        [contentView addSubview:stoneobs_ui_button];
        stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [stoneobs_ui_button setImage:ST_IMAGE_NAMED(@"video_评论") forState:UIControlStateSelected];
        stoneobs_ui_button.right = UISCREEN_WIDTH - 15;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            [weakSelf onSlectedLookCommentButton];
        }];
        stoneobs_ui_button.bottom = top;
        top = stoneobs_ui_button.top;
        stoneobs_ui_label.centerX = stoneobs_ui_button.centerX;
    });
    
    ({
        
        STLabel * stoneobs_ui_label = [[STLabel alloc] initWithFrame:CGRectMake(0, 0, 40, 20)
                                                                text:@(self.model.up_num).description
                                                           textColor:UIColor.whiteColor
                                                                font:12
                                                         isSizetoFit:NO
                                                       textAlignment:NSTextAlignmentCenter];
        [contentView addSubview:stoneobs_ui_label];

        self.goodLabel = stoneobs_ui_label;
        stoneobs_ui_label.bottom = top;
        top = stoneobs_ui_label.top;
        
        
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, top, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:20
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:ST_IMAGE_NAMED(@"video_赞")];
        [contentView addSubview:stoneobs_ui_button];
        stoneobs_ui_button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
        [stoneobs_ui_button setImage:ST_IMAGE_NAMED(@"treehole_赞") forState:UIControlStateSelected];
        stoneobs_ui_button.selected = self.model.is_up;
        stoneobs_ui_button.right = UISCREEN_WIDTH - 15;
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            
            [THTreeHoleDataController addGoodOrCancleFormPosts_id:weakSelf.model.t_id handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                
                sender.selected = !sender.selected;
                weakSelf.model.is_up =  !weakSelf.model.is_up;
                if (weakSelf.model.is_up) {
                    weakSelf.model.up_num = weakSelf.model.up_num+1;
                }else{
                    weakSelf.model.up_num = weakSelf.model.up_num-1;
                }
                weakSelf.goodLabel.text = @(weakSelf.model.up_num).description;
            }];
        }];
        stoneobs_ui_button.bottom = top;
        top = stoneobs_ui_button.top;
        stoneobs_ui_label.centerX = stoneobs_ui_button.centerX;
    });
    
    ({
        STButton * stoneobs_ui_button = [[STButton alloc] initWithFrame:CGRectMake(0, top, 40, 40)
                                                                  title:nil
                                                             titleColor:nil
                                                              titleFont:0
                                                           cornerRadius:20
                                                        backgroundColor:nil
                                                        backgroundImage:nil
                                                                  image:nil];
        [contentView addSubview:stoneobs_ui_button];
        stoneobs_ui_button.right = UISCREEN_WIDTH - 15;
        [stoneobs_ui_button sd_setImageWithURL:self.model.user_header_img.mj_url forState:UIControlStateNormal];
        [stoneobs_ui_button setClicAction:^(UIButton *sender) {
            [TMUtils goto_user_info_witdh_id:weakSelf.model.user_id];
        }];
        [stoneobs_ui_button stbw_setBorderWith:2 borderColor:UIColor.whiteColor cornerRadius:20];
        stoneobs_ui_button.bottom = top - 30;
        top = stoneobs_ui_button.top;
        
        UIImageView * stoneobs_ui_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, stoneobs_ui_button.bottom, 15, 15)];
        stoneobs_ui_imageView.image = ST_IMAGE_NAMED(@"video_+");
        stoneobs_ui_imageView.centerX = stoneobs_ui_button.centerX;
        [contentView addSubview:stoneobs_ui_imageView];
        stoneobs_ui_imageView.centerY = stoneobs_ui_button.bottom;
        
        
        top = stoneobs_ui_button.bottom + 25;
    });
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
}
#pragma mark --stoneobs Action Method
- (void)stoneobs_fun_st_rightBarAction:(id)sender{
    
    NSString * title = @"收藏";
    if (self.model.is_collection) {
        title = @"取消收藏";

    }
    NSArray * array = @[title,@"举报"];
    THUserModel * mine = STTools_UserManger.defult.stoneobs_fun_get_loginedUser;
    if ([self.model.user_id isEqualToString:mine.uid]) {
        array = @[title,@"编辑"];
    }
    __weak typeof(self) weakSelf =  self;
    STTools_ActionSheet * sheet = [STTools_ActionSheet stoneobs_fun_actionSheetWithTitle:@"操作选项" buttonTitles:array cancelTitle:@"取消" handle:^(NSInteger index) {
        if (index >= array.count) {
            return;
        }
        NSString * title = array[index];
        if ([title containsString:@"收藏"]) {
            [THTreeHoleDataController addCollectionOrCancleFormPosts_id:self.model.t_id handle:^(NSError *error, BOOL success, NSDictionary *resp) {
                self.model.is_collection = !self.model.is_collection;
                [SVProgressHUD showSuccessWithStatus:@"操作成功"];
            }];
        }
        if ([title isEqualToString:@"举报"]) {
            
            dian_ji_Tools_AddReProportMJ_SViewController * vc = dian_ji_Tools_AddReProportMJ_SViewController.new;
            vc.to_id = self.model.t_id;
            [weakSelf pushViewController:vc animated:1];
        }
        
        if ([title isEqualToString:@"编辑"]) {
            
            THAddVideoViewController * vc = THAddVideoViewController.new;
            vc.model = self.model;
            [weakSelf pushViewController:vc animated:1];
        }
        
    }];
    [sheet show];

    
}

- (void)onSlectedCommentButton{
    THAddCommentPresentViewController * vc  = THAddCommentPresentViewController.new;
    [vc setDidFillComment:^(NSString * _Nonnull comment) {
        [THTreeHoleDataController addcommentFormPosts_id:self.model.t_id content:comment handle:^(NSError *error, BOOL success, NSDictionary *resp) {
            
            if (success) {
                self.model.comment_num = self.model.comment_num+1;
                self.commentLabel.text = @(self.model.comment_num).description;
            }
        }];
    }];
    [self presentViewController:vc animated:NO completion:nil];
    
//    THAddcommentViewController *vc = THAddcommentViewController.new;
//    [vc setDidFillComment:^(NSString * _Nonnull comment) {
//        [THTreeHoleDataController addcommentFormPosts_id:self.model.t_id content:comment handle:^(NSError *error, BOOL success, NSDictionary *resp) {
//
//            if (success) {
//                self.model.comment_num = self.model.comment_num+1;
//                self.commentLabel.text = @(self.model.comment_num).description;
//            }
//        }];
//    }];
//    [self pushViewController:vc animated:1];
}
- (void)onSlectedLookCommentButton{
    __weak typeof(self) weakSelf =  self;
    THLookVideoCommentViewController * vc = THLookVideoCommentViewController.new;
    vc.model = self.model;
    vc.vc = weakSelf;
    [vc setDidComment:^{
        self.model.comment_num = self.model.comment_num+1;
        self.commentLabel.text = @(self.model.comment_num).description;
    }];
    [self presentViewController:vc animated:NO completion:nil];
}
@end
